package com.fw.controller;

import com.alibaba.fastjson.JSONObject;
import com.fw.entity.*;
import com.fw.enums.OrderState;
import com.fw.service.IMeatService;
import com.fw.service.IMorderService;
import com.fw.service.IMorelationService;
import com.fw.service.IUserService;
import com.fw.utils.JWTUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RequestMapping("/retailer")
@RestController
public class RetailerController {
    @Resource
    IMorderService morderService;
    @Resource
    IMeatService meatService;
    @Resource
    IUserService userService;
    @Resource
    IMorelationService morelationService;

    @RequestMapping("/listMeatInOrder")
    public Result listMeatInOrder(HttpServletRequest request)throws MyException {
        String obuyuid = JWTUtils.getUserId(request.getHeader("token"));
        Map<String, Object> m = new HashMap<>();
        m.put("obuyuid", obuyuid);
        List<Morder> morders = morderService.listByMap(m);
        String[] types = {"里脊", "排骨", "五花肉", "内脏", "肘子"};
        for (Morder morder:
             morders) {
            Map<String, Object> ans = new HashMap<>();
            Map<String, Object> searchMap = new HashMap<>();
            searchMap.put("oid", morder.getOid());
            List<Morelation> morelations = morelationService.listByMap(searchMap);
            List<Integer> ids = new ArrayList<>();
            for (Morelation morelation :
                    morelations) {
                ids.add(morelation.getMid());
            }
            System.out.println(morelations);
            System.out.println(ids);
            List<Meat> meatList = meatService.listByIds(ids);
            int []nums = {0,0,0,0,0};
            for (Meat meat :
                    meatList) {
                if(meat.getMtype().equals(types[0]))
                    nums[0]++;
                else if(meat.getMtype().equals(types[1]))
                    nums[1]++;
                else if(meat.getMtype().equals(types[2]))
                    nums[2]++;
                else if(meat.getMtype().equals(types[3]))
                    nums[3]++;
                else if(meat.getMtype().equals(types[4]))
                    nums[4]++;
            }
            for(int i = 0; i < 5; i++){
                ans.put(types[i], nums[i]);
            }
            morder.setM(ans);
            morder.setOsellname(userService.getById(morder.getOselluid()).getUname());
        }
        System.out.println(morders);
        return new Result(morders,"获取所有订单成功","000000");
    }

    @RequestMapping("/meatIn")
    public Result meatIn(String oid,
                         HttpServletRequest request)throws MyException{
        String obuyuid = JWTUtils.getUserId(request.getHeader("token"));
        if((oid + "").equals("")){
            throw new MyException("订单号不能为空","050201");
        }
        Map<String, Object> m = new HashMap<>();
        m.put("oid",oid);
        System.out.println(m);
        List<Morder> morders = morderService.listByMap(m);
        if(morders.isEmpty()){
            throw new MyException("该订单不存在","050202");
        }
        Morder morder =morders.get(0);
        if(!morder.getObuyuid().toString().equals(obuyuid)){
            throw new MyException("该订单不属于此零售商","050204");
        }
        if(morder.getOstate().equals(OrderState.FINISH.getValue())){
            throw new MyException("该订单已入场","050203");
        }
        if(morder.getOstate().equals(OrderState.START.getValue())){
            throw new MyException("该订单未出场","050205");
        }

        if(morder.getOstate().equals(OrderState.OUT.getValue())){
            morder.setOstate(OrderState.FINISH.getValue());
            Date date = new Date();
            morder.setIndate(date);
            morderService.saveOrUpdate(morder);
            m.clear();
        }
        return new Result(morder,"订单签收成功","000000");
    }

    @RequestMapping("/listAllMeat")
    public Result listAllMeat(HttpServletRequest request){
        String mowneruid = JWTUtils.getUserId(request.getHeader("token"));
        Map<String, Object> m= new HashMap<>();
        m.put("mowneruid", mowneruid);
        List<Meat> meatList = meatService.listByMap(m);
        return new Result(meatList,
                "获取所有猪肉成功","000000");
    }

    public List<Meat> getmeat(Map<String, Object> m,String mowneruid){
        System.out.println(m);
        List<Meat> meats = meatService.listByMap(m);
        m.remove("mowneruid");
        int size = meats.size();
        m.put("mnum",size);
        double mtotalweight = 0;
        for (Meat meat : meats) {
            mtotalweight += meat.getMweight();
        }
        m.put("mtotalweight",mtotalweight);
        return meats;
    }

    @RequestMapping("/updateMeatState")
    public Result updateMeatState(@RequestParam(value = "mid") String mid1,
                                  @RequestParam(value = "mweight") String mweight1,
                                  HttpServletRequest request)throws MyException{
        String mowneruid = JWTUtils.getUserId(request.getHeader("token"));
        Map<String, Object> m = new HashMap<>();
        m.put("mowneruid", mowneruid);
        m.put("mid",mid1);
        System.out.println(m);
        if(mid1.equals("")){
            throw new MyException("猪肉id不能为空","050401");
        }
        if(mweight1.equals(""))
            throw new MyException("订单id不能为空", "050402");
        int mid = Integer.parseInt(mid1);
        double mweight = Double.parseDouble(mweight1);
        if(mweight <= 0)
            throw new MyException("猪肉数量错误", "050403");
        List<Meat> meatList = meatService.listByMap(m);
        if(meatList.isEmpty())
            throw new MyException("该订单不存在", "050404");
        Meat meat = meatList.get(0);
        if(meat.getMweight() <= 0)
            throw new MyException("该份猪肉已卖完", "050405");
        if(meat.getMweight() < mweight)
            throw new MyException("该份猪肉数量不足", "050406");
        meat.setMweight(meat.getMweight() - mweight);
        meatService.saveOrUpdate(meat);
        return new Result(null, "猪肉状态修改成功", "000000");
    }
}
